import sys
import os

DefaultDirFrom = r'C:\EMC\ManagementServer\7.31.25.0.66.1\ECOMCIMOM'
myFileExt = r'.dll.liangr'
dllExt = r'.dll'
orgFileExt = r'.dll.org'
files2Find = ['NaviContextManager', 'NonFipsUtilities', 'ServerProvider']

liangr2Org = True

def FireCopy(srcName, desName):
    command = 'move %s %s' % (srcName, desName)
    print(command)
    if os.system(command) == 0:
        print('Successful move from ', srcName, ' to ', desName)
    else:
        print('Fail to move ', srcName, ' to ', desName)
    
def FindRecur(root):
    try:
        for parent, dirNames, fileNames in os.walk(root):
            for fileName in fileNames:
                for file2Find in files2Find:
                    if fileName == file2Find + myFileExt and os.path.exists(os.path.join(parent, file2Find + dllExt)):
                        FireCopy(os.path.join(parent, file2Find + dllExt), os.path.join(parent, file2Find + orgFileExt))
                        FireCopy(os.path.join(parent, fileName), os.path.join(parent, file2Find + dllExt))
                    if fileName == file2Find + orgFileExt and os.path.exists(os.path.join(parent, file2Find + dllExt)):
                        FireCopy(os.path.join(parent, file2Find + dllExt), os.path.join(parent, file2Find + myFileExt))
                        FireCopy(os.path.join(parent, fileName), os.path.join(parent, file2Find + dllExt))
    except Exception as ex:
        print('Exception: ' + ex)
                        
def main():
    dirFrom = DefaultDirFrom
    if len(sys.argv) > 2: 
        print('Arguments Error: Too many arguments!')
    if len(sys.argv) == 2:
        dirFrom = sys.argv[1]
    FindRecur(dirFrom)


if __name__ == "__main__":
    main()
